package com.lms.logistics_management_system.controller;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lms.logistics_management_system.common.Result;
import com.lms.logistics_management_system.entity.Combo;
import com.lms.logistics_management_system.entity.Doctor;
import com.lms.logistics_management_system.entity.Item;
import com.lms.logistics_management_system.entity.User;
import com.lms.logistics_management_system.service.ComboService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api
@Slf4j
@RestController
@RequestMapping("/combo")
public class ComboController {
    @Autowired
    private ComboService comboService;

    @ApiOperation("获取所有套餐及对应项目信息")
    @GetMapping("/get")
    public Result findAllComboItem(){
        return Result.success(comboService.getAllComboItem());
    }

    @ApiOperation("管理员添加套餐")
//    @ApiImplicitParam(name = "itemIdList",required = false)
//    @ApiImplicitParams({
//    @ApiImplicitParam(name="comboId",dataType="int", paramType = "query", example="5",required = true),
//    @ApiImplicitParam(name="comboPrice",dataType="double",paramType = "query", example="80.0",required = true),
//            @ApiImplicitParam(name="itemIdList",dataType="int",allowMultiple = true, paramType = "query",example="[8,9]",required = true)})
    @PostMapping("/add")
    public Result add( @RequestParam Integer comboId, @RequestParam double comboPrice,@RequestParam(required = false) int[] itemIdList){
        return comboService.addCombo(comboId,comboPrice, itemIdList);
    }

    @ApiOperation("管理员修改套餐，,itemIdList设null时为修改价格功能")
    @PostMapping("/update")
    public Result update(@RequestParam Integer comboId,@RequestParam double comboPrice, @RequestParam(required = false) int[] itemIdList){
        return comboService.updateCombo(comboId,comboPrice,itemIdList);
    }

    @ApiOperation("按id搜索套餐")
    @GetMapping("/getComboById")
    public Result getComboInfo(@RequestParam Integer comboId){
        return Result.success(comboService.getComboItemById(comboId));
    }

    @ApiOperation("管理员删除套餐")
    @DeleteMapping("/del")
    public boolean delete(@RequestParam Integer comboId){return comboService.removeByComboId(comboId);}

    @ApiOperation("管理员批量删除套餐")
    @DeleteMapping("/del/bash")
    public boolean deleteBash(@RequestParam List<Integer> comboIds){return comboService.removeByComboIds(comboIds);}


//    @ApiOperation("查询所有套餐的id和price")
//    @GetMapping
//    public Result findAll(){
//        return Result.success(comboService.list());
//    }



    //@ApiOperation("管理员操作，新增和修改放一起")
//    @GetMapping("/save")
//    public boolean add(@RequestBody Integer comboId,
//                       @RequestBody Integer itemId,
//                       @RequestBody double comboPrice) {
//        Combo combo = new Combo();
//        combo.setComboId(comboId);
//        combo.setItemId(itemId);
//        combo.setComboPrice(comboPrice);
//        return ComboService.saveCombo(combo);
//    }
//}


//    //mybitis-plus方式
//    @GetMapping("/page")
//    public IPage<User> findPage(@RequestParam Integer pageNum,
//                                @RequestParam Integer pageSize,
//                                @RequestParam (defaultValue = "")String search){
//        //若没有传值，则赋默认值，防止将所有数据筛选出来
//        IPage<Doctor> page=new Page<>(pageNum,pageSize);
//        QueryWrapper queryWrapper=new QueryWrapper<Doctor>();
//        queryWrapper.like("doctor_id",search);
//        queryWrapper.or();
//        queryWrapper.like("doctor_name",search);
//        queryWrapper.or();
//        queryWrapper.like("doctor_email",search);
//        queryWrapper.or();
//        queryWrapper.like("doctor_phone_number",search);
//        queryWrapper.or();
//        queryWrapper.like("department_id",search);
//        queryWrapper.or();
//        queryWrapper.like("room_id",search);
//        queryWrapper.orderByDesc("doctor_id");
//        //后台获取用户登录信息
//        //Doctor currentDoctor= TokenUtils.getUserInfo();
//        //System.out.println("=========================当前用户信息:"+currentUser.getNickname());
//        return doctorService.page(page,queryWrapper);
//    }

//    /*
//     * 导出功能，导出当前页面中load的对象
//     * */
//    @GetMapping("/export")
//    public void export(HttpServletResponse response)throws Exception{
//        //从数据库查出数据
//        List<Doctor>list = doctorService.list();
//        System.out.println(list.get(0).getDoctorName());
//        //创建工具类writer，写出到浏览器
//        ExcelWriter writer = ExcelUtil.getWriter(true);
//        //自定义标题名
//        writer.addHeaderAlias("doctorId","工号");
//
//        //将list对象一次性写入到excel中，使用默认样式，强制输出标题
//        writer.write(list,true);
//        //设置浏览器响应的格式固定写法
//        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
//        Date nowtime=new Date();
//        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
//        String nowtimstr=sdf.format(nowtime);//Date类型转换为字符串
//        String fileName = URLEncoder.encode("医生信息"+nowtimstr,"UTF-8");
//        response.setHeader("Content-Disposition","attachment;filename="+fileName+".xlsx");
//
//        //获取输出流
//        ServletOutputStream out = response.getOutputStream();
//        writer.flush(out,true);
//        out.close();
//        writer.close();
//    }

    /*
     *
     * excel导入接口
     * @prama
     * @throws Exception
//     * */
//    @PostMapping("/import")
//    public boolean imp(MultipartFile file)throws Exception{
//        InputStream inputStream=file.getInputStream();
//        ExcelReader reader = ExcelUtil.getReader(inputStream);
//        List<Doctor> list=reader.readAll(Doctor.class);
//        return doctorService.saveBatch(list);
//    }
//
//    @PostMapping("/token")
//    public Result token(){return Result.success();}
//
//    /**
//     * 获取医生个人信息接口
//     * @prama doctorId
//     * @return Result
//     * */
//    @GetMapping("/getDoctorInfo")
//    public Result getDoctorInfo(@RequestParam Integer doctorId){
//        Map<String,Object>res=new HashMap<>();
//        Doctor doctor=doctorMapper.getByDoctorId(doctorId);
//        res.put("doctor",doctor);
//        String departmentName = doctorMapper.getDepartmentName(doctor.getDepartmentId());
//        res.put("departmentName",departmentName);
//        return Result.success(res);
//    }
//
//    /**
//     * 获取医生个人信息接口
//     * @prama doctorId
//     * @return Result
//     * */
//    @PostMapping("/changePassword")
//    public Result changePassword(@RequestParam Integer doctorId,
//                                 @RequestParam String password,
//                                 @RequestParam String newpassword){
//        return doctorService.changePassword(doctorId,password,newpassword);
//    }


}

